package com.caoheng.oms.controller;


import com.caoheng.core.common.Result;
import com.caoheng.core.util.ResultUtil;
import com.caoheng.entity.MerchantInfo;
import com.caoheng.oms.service.IMerchantInfoService;
import com.caoheng.oms.vo.MerchantInfoBody;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.caoheng.core.util.ResultUtil.*;

@Api(tags = "商户信息管理")
@RestController
@RequestMapping("merchant")
public class MerchantInfoController {

    @Autowired
    private IMerchantInfoService merchantInfoService;

    @ApiOperation(value = "添加商户")
    @PostMapping("/create")
    public Result create(@Validated  MerchantInfoBody body){
        int effectNum = merchantInfoService.addMerchantInfo(body.convert());
        return effectNum <= 0 ? error("商户信息添加失败！"): ok();
    }

    @ApiOperation(value = "编辑商户信息")
    @PutMapping("/edit/{id}")
    public Result edit(@PathVariable String id,MerchantInfoBody body){
        MerchantInfo merchantInfo = merchantInfoService.getMerchantInfo(id);
        if(merchantInfo == null){
            return error("该商户不存在!");
        }
        body.convert(merchantInfo);
        int effectNum;
        effectNum = merchantInfoService.updateMerchantInfo(merchantInfo);
        return effectNum <= 0 ? error("商户信息编辑失败！"): ok();
    }

    @ApiOperation(value = "获取商户信息")
    @GetMapping("/{id}")
    public Result get(@PathVariable String id){
        MerchantInfo merchantInfo = merchantInfoService.getMerchantInfo(id);
        return merchantInfo != null ? ok(merchantInfo) : error("商户信息不存在!");
    }


    @ApiOperation(value = "商户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",paramType = "query",dataType = "int",defaultValue = "0",required = true),
            @ApiImplicitParam(name = "size",paramType = "query",dataType = "int",defaultValue = "0",required = true)}
    )
    @GetMapping("/list")
    public Result list(Integer page,Integer size){
        return ResultUtil.ok( PageHelper.startPage(page,size).doSelectPageInfo(()->merchantInfoService.getMerchantInfoList()));
    }

}
